package com.travel.Controller.User;

import com.github.pagehelper.PageInfo;
import com.travel.Bean.Strategy;
import com.travel.JPA.StrategyJPA;
import com.travel.JPA.User_upJPA;
import com.travel.Service.Impl.CultureServiceImpl;
import com.travel.Service.Impl.ManagerServiceImpl;
import com.travel.Service.Impl.StrategyServiceImpl;
import com.travel.Service.Impl.User_upServiceImpl;
import com.travel.util.ConsoleOutputControl;
import com.travel.util.R;
import com.travel.util.SecurltyInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("攻略表")
@RestController

@RequestMapping("/UserStrategy")
public class UserStrategyController {

    @Autowired
    private StrategyServiceImpl strategyService;
    @Autowired
    private User_upServiceImpl user_upService;
    @Autowired
    private ManagerServiceImpl managerService;
    @Autowired
    private SecurltyInfo securltyInfo;


    /**
     * 得到攻略列表
     * 得到策略列表
     *
     * @param ticketId      项目id
     * @param page          页面
     * @param limit         记录条数
     * @param strategyTitle 攻略标题
     * @return {@link R}
     */
    @ApiOperation("获取攻略列表")
    @PostMapping("getStrategyList")
    public R getStrategyList(Integer ticketId,Integer cultureId,String strategyTitle, @RequestParam(defaultValue = "1")Integer page,
                             @RequestParam(defaultValue = "15")Integer limit){
        if (securltyInfo.getAdminRole().indexOf("admin")>0)
            cultureId = managerService.getCultureIdByManagerName(securltyInfo.getUsername());

        PageInfo<Strategy> pageInfo = strategyService.getStrategyList(ticketId,cultureId,strategyTitle,page,limit);
        return R.ok("0",pageInfo.getList(),pageInfo.getTotal());
    }


    /**
     * 保存攻略
     *
     * @param strategy 攻略
     * @return {@link R}
     */
    @ApiOperation("发布攻略")
    @PostMapping("saveStrategy")
    public R saveStrategy(@RequestBody Strategy strategy){

        if(strategyService.saveStrategy(strategy)){
            return R.ok(0);
        }
        return R.no(1);
    }


    /**
     * 看到strategy
     *
     * @param strategyId strategy id
     * @return {@link R}
     */
    @ApiOperation("查看攻略内容")
    @PostMapping("seestrategy")
    public R seestrategy(Integer strategyId){
        Strategy strategy = strategyService.findStrategyById(strategyId);
        if(null == strategy){
            return R.no(1,"攻略不存在");
        }

        return R.ok(1,strategy);
    }

    @ApiOperation("获取攻略的点赞数量")

    @PostMapping("/getStrategy_Up")
    public R getStrategy_up(Integer strategyId){
        Integer upNum = strategyService.findStrategy_up(strategyId);
        if(upNum < 0){
            return R.no(-1,"获取点赞失败");
        }
        return R.ok(1,upNum);
    }


    /**
     * 攻略点赞
     *
     * @param strategyId 攻略id
     * @param userId     用户id
     * @return {@link R}
     */
    @ApiOperation("点赞攻略")


    @PostMapping("strategy_Up")
    public R strategy_Up(Integer strategyId,String userId){
        userId = userId == null ? securltyInfo.getUsername() : userId;
        try {
            //检查是否已点赞
            if(user_upService.strategy_up_YN(userId,strategyId)){
                //取消点赞
                if(strategyService.strategy_up(strategyId,-1))
                  return R.ok(1,"取消点赞");
            }else {
                //点赞
                if(strategyService.strategy_up(strategyId,1))
                    return R.ok(0,"点赞成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            ConsoleOutputControl.getOutputString_33(" strategy_Up error");
            return R.no(-1,"点赞失败");
        }
        return R.ok(-2,"操作失败");
    }

}
